<?php

namespace app\admin\model;

use think\facade\Config;
use think\Model;

class Brand extends Model{
    public function getIfShowAttr($value){
        if (Config::has('param.param_if_show')){
            $config = Config::get('param.param_if_show');
            return $config[$value];
        }
    }
    public function getRecommendedAttr($value,$data){
        if (Config::has('param.param_recommended')){
            $config1 = Config::get('param.param_recommended');
            return $config1[$data['recommended']];
        }
    }
    public function searchKeywordsAttr($query,$value,$data){
        if(''!==$value){
            $query->whereLike('brand_name','%'.$value.'%');
        }

    }
    public function searchIfShowAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("if_show",$value);
        }
    }
    public function searchIsHotAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("recommended",$value);
        }
    }
    public function searchTpAttr($query,$value){
        if(isset($value) && $value==1){
            $query->where("brand_logo","<>",'');
            $query->where("brand_logo","<>",null);
        }elseif (isset($value) && $value==2){
            $query->where("brand_logo","");
            $query->whereOr("brand_logo",null);
        }
    }
    public static function pin($data=[],$query=[],$page=true){
        $model = self::field(true)->withSearch(array_keys($data),$data);
//        if(isset($data['keywords']) && '' !== $data['keywords']){
//            $model->whereLike('brand_name',"%{$data['keywords']}%");
//        }
//        if(isset($data['is_hot']) && '' !== $data['is_hot']){
//            $model->where("recommended",$data['if_hot']);
//        }
//        if(isset($data['if_show']) && '' !== $data['if_show']){
//            $model->where("if_show",$data['if_show']);
//        }
//        if(isset($data['tp']) && $data['tp']==1){
//            $model->where("brand_logo","<>",'');
//        }elseif (isset($data['tp']) && $data['tp']==2){
//            $model->where("brand_logo","");
//        }
        if($page){
            return $model->paginate([
                'list_roes'=>10,
                'query'=> $query
            ]);
        }
        return $model->select();
    }
}